//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <kcrt.h>
#include <util.h>
#include <bsp.h>
#include "_sprintf.h"

#ifdef _neptune
#include <zenr_api.h>
static char s_buf[512];
#endif

print_t _print = &BspWriteConsole;

EXTERN_C int  CDECL vkprintf(const char *, va_list);

EXTERN_C int CDECL kprintf(const char *fmt,...)
{
//#ifdef _neptune
//#define STACKLIMIT 0x10c00000
//    uint_t nsp;
//    if ((uint_t)&nsp < STACKLIMIT) {
//        nu_puts("===================================\n");
//        nu_puts("Error Nucleus Callback Use Kprintf!\n");
//        nu_puts(fmt);
//        nu_puts("===================================\n");
//        while (1);
//    }
//#endif

    va_list ap;

    va_start(ap, fmt);

    int nLength = _vprintf(_print, fmt, ap);

    va_end(ap);

    return nLength;
}

EXTERN int CDECL vkprintf(const char *fmt, va_list ap)
{
    return _vprintf(_print, fmt, ap);
}

EXTERN_C int CDECL _printf(print_t print, const char *fmt,...)
{
    va_list ap;

    va_start(ap, fmt);

    int nLength = _vprintf(print, fmt, ap);

    va_end(ap);

    return nLength;
}

EXTERN_C int CDECL _vprintf(print_t print, const char *fmt, va_list ap)
{
#ifndef ELASTOS_RC   
#ifdef _neptune
    int nLength = vsprintf(s_buf, fmt, ap);
    print(s_buf);
#else
    char szBuf[512];
    int nLength = vsprintf(szBuf, fmt, ap);
    print(szBuf);
#endif

    return nLength;
#else
    return 0;
#endif //ELASTOS_RC        
}
